
import requests
import unittest
from api.LoginApi import LoginApi

class TestLoginApi(unittest.TestCase):

    def setUp(self):
        self.login_api = LoginApi()
        self.session = requests.session()

    def tearDown(self):
        if self.session:
            self.session.close()

    def test01_login_success(self):
        response = self.login_api.get_verify_code(self.session)
        self.assertEqual(200,response.status_code)
        self.assertIn("image",response.headers.get( "Content-Type"))

        response = self.login_api.login(self.session,"13111111111","1234","8888")
        print("test01_login_success的login接口返回的数据：",response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, response.json().get("status"))
        self.assertIn("登陆成功", response.json().get("msg"))

    def test02_account_is_not_exist(self):
        response = self.login_api.get_verify_code(self.session)
        self.assertEqual(200, response.status_code)
        self.assertIn("image", response.headers.get("Content-Type"))

        response = self.login_api.login(self.session, "13222222222", "1234", "8888")
        print("test01_login_success的login接口返回的数据：", response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(-1, response.json().get("status"))
        self.assertIn("账号不存在", response.json().get("msg"))

    def test03_password_error(self):
        response = self.login_api.get_verify_code(self.session)
        self.assertEqual(200, response.status_code)
        self.assertIn("image", response.headers.get("Content-Type"))

        response = self.login_api.login(self.session, "13111111111", "123456", "8888")
        print("test01_login_success的login接口返回的数据：", response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(-2, response.json().get("status"))
        self.assertIn("密码错误", response.json().get("msg"))